package com.xtayfjpk.zookeeper.test.master_workers.callback.client;

import org.apache.log4j.Logger;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.data.Stat;

import com.xtayfjpk.zookeeper.test.master_workers.Client;
import com.xtayfjpk.zookeeper.test.master_workers.callback.BaseStatCallback;

public class TaskExistsCallback extends BaseStatCallback {
	private static final Logger LOGGER = Logger.getLogger(TaskExistsCallback.class);
	private Client client;
	public TaskExistsCallback(Client client) {
		this.client = client;
	}
	
	@Override
	protected void connectionLossCaseAction(int rc, String path, Object ctx, Stat stat) {
		client.watchTaskStatus(path, ctx);
	}
	
	@Override
	protected void okCaseAction(int rc, String path, Object ctx, Stat stat) {
		//如果stat为null则说明znode不存在
		if(stat!=null) {
			client.getTaskResult(path);
			LOGGER.info("Status node is there: " + path);
		}
	}
	
	@Override
	protected void defaultCaseAction(int rc, String path, Object ctx, Stat stat) {
		LOGGER.error("Something went wrong when " +
        		"checking if the status node exists: " + 
                KeeperException.create(Code.get(rc), path));
	}
}
